package cn.signalpha.modules.shop.wrapper;

import cn.signalpha.core.mp.support.BaseEntityWrapper;
import cn.signalpha.core.tool.constant.SystemConstant;
import cn.signalpha.core.tool.node.ForestNodeMerger;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.SpringUtil;
import cn.signalpha.modules.shop.entity.SellerGoodsCategory;
import cn.signalpha.modules.shop.service.SellerGoodsCategoryService;
import cn.signalpha.modules.shop.vo.SellerGoodsCategoryVO;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 包装类,返回视图层所需的字段
 */
public class SellerGoodsCategoryWrapper extends BaseEntityWrapper<SellerGoodsCategory, SellerGoodsCategoryVO> {

	private static final SellerGoodsCategoryService sellerGoodsCategoryService;

	static {
		sellerGoodsCategoryService = SpringUtil.getBean(SellerGoodsCategoryService.class);
	}

	public static SellerGoodsCategoryWrapper build() {
		return new SellerGoodsCategoryWrapper();
	}

	@Override
	public SellerGoodsCategoryVO entityVO(SellerGoodsCategory sellerGoodsCategory) {
		SellerGoodsCategoryVO sellerGoodsCategoryVO = Objects.requireNonNull(BeanUtil.copy(sellerGoodsCategory, SellerGoodsCategoryVO.class));
		if(Func.equals(sellerGoodsCategory.getParentId(), SystemConstant.TOP_PARENT_ID)) {
			sellerGoodsCategoryVO.setParentName(SystemConstant.TOP_PARENT_NAME);
		}else{
			SellerGoodsCategory parent = sellerGoodsCategoryService.getById(sellerGoodsCategory.getParentId());
			sellerGoodsCategoryVO.setParentName(parent.getName());
		}
		return sellerGoodsCategoryVO;
	}

	public List<SellerGoodsCategoryVO> listNodeVO(List<SellerGoodsCategory> list) {
		List<SellerGoodsCategoryVO> collect = list.stream().map(item -> BeanUtil.copy(item, SellerGoodsCategoryVO.class)).collect(Collectors.toList());
		return ForestNodeMerger.merge(collect);
	}

	public List<SellerGoodsCategoryVO> listNodeLazyVO(List<SellerGoodsCategoryVO> list) {
		return ForestNodeMerger.merge(list);
	}

}
